<?php

if (! function_exists ( 'GetViewAsTable' )) {
	/**
	 * $array as array( Name => array(Name，名称，类型，值，单位) )
	 * @param array $array
	 * @param string $name
	 * @param htmlstring $name
	 */
	function GetViewAsTable($array, $name = '', $params = '') {
		if (empty ( $array ))
			return;
		
		$html = '<table width="100%" border="1" cellspacing="0" cellpadding="3">';
		if (! empty ( $name )) {
			$html .= "<caption>$name</caption>";
		}
		foreach ( $array as $v ) {
			if (preg_match('/^.+_image$/i',$v[0])){
				$v[3] = GetImage($v[3]);
			}
			$html .= "<tr><th scope=\"row\" bgcolor=\"#ddeeff\">$v[1]</th><td>$v[3]&nbsp;$v[4]</td></tr>";
		}
		$html .= $params;
		$html .= '</table>';
		
		return $html;
	}
}

if (! function_exists ( 'ShowViewAsTableA' )) {
	function ShowViewAsTableA($arr, $name = '', $alert = '', $params = '') {
		$html = '';
		$html .= GetViewAsTable ( $arr, GetTableComment ( $name ), $params );
		$html .= $alert;
		echo $html;
	}
}

if (! function_exists ( 'ShowViewAsTableB' )) {
	function ShowViewAsTableB($arr, $name = '', $alert = '', $params = '') {
		$html = '';
		$html .= $alert;
		$html .= GetViewAsTable ( $arr, GetTableComment ( $name ), $params );
		echo $html;
	}
}

if (! function_exists ( 'GetInputAsTable' )) {
	/**
	 * $array as array( Name => array(Name，名称，类型，值，单位) )
	 * @param array $array
	 * @param string $name
	 * @param htmlstring $name
	 */
	function GetInputAsTable($array, $name = '', $params = '') {
		if (empty ( $array ))
			return;
		
		$html = '<table width="100%" border="1" cellspacing="0" cellpadding="3">';
		if (! empty ( $name )) {
			$html .= "<caption>$name</caption>";
		}
		foreach ( $array as $v ) {
			$html .= "<tr><th scope=\"row\" bgcolor=\"#ddeeff\">$v[1]</th><td>" . TurnInputType ( $v ) . "&nbsp;$v[4]</td></tr>";
		}
		$html .= $params;
		$html .= '</table>';
		
		return $html;
	}
}

if (! function_exists ( 'ShowInputAsTableA' )) {
	function ShowInputAsTableA($arr, $name = '', $act = '', $params = '') {
		$html = '';
		$html .= GetInputAsTable ( $arr, GetTableComment ( $name ), $params );
		$html .= GetSubmit ();
		$html .= GetReset ();
		$html = GetForm ( $html, $act );
		echo $html;
	}
}

if (! function_exists ( 'ShowInputAsTableB' )) {
	function ShowInputAsTableB($arr, $name = '', $alert = '', $act = '', $params = '') {
		$html = '';
		$html .= $alert;
		$html .= GetInputAsTable ( $arr, GetTableComment ( $name ), $params );
		$html .= GetSubmit ();
		$html .= GetReset ();
		$html = GetForm ( $html, $act );
		echo $html;
	}
}
if (! function_exists ( 'TurnInputType' )) {
	/**
	 * $a as array(Name，名称，类型，值，单位)
	 * @param array $a
	 */
	function TurnInputType($a) {
		switch (strtolower ( $a [2] )) {
			case 'int' :
				$r = GetInput ( $a [0], $a [3] );
				break;
			case 'varchar' :
				$r = GetInput ( $a [0], $a [3] );
				break;
			case 'text' :
				$r = GetTextArea ( $a [0], $a [3] );
				break;
			case 'select' :
				$r = GetSelect( $a[0], $a[3] );
				break;
			case 'list' :
				$r = GetSelect( $a[0], $a[3], 5);
				break;
			default :
				$r = GetInput ( $a [0], $a [3] );
		}
		if (preg_match('/^.+_image$/i',$a[0])){
			$r = GetFile($a[0],$a[3],'accept="image/*"');
		}
		return $r;
	}
}

if (! function_exists ( 'ViewListAsTableA' )) {
	/**
	 * show formated list
	 * @param array $keyinfo get from GetFieldComment() like array( name => comment ) 
	 * @param array $value get from database like array( index => array( name => value ) )
	 * @param array $unit like array( name => value )
	 * @param string $param
	 */
	function ViewListAsTableA($keyinfo, $value, $unit = array(), $param = '') {
		$p = basename ( $_SERVER ['PHP_SELF'] );
		$pb = substr ( $p, 0, - 4 );
		$op = $pb . '_action.php';
		
		$html = '<table width="100%" border="1" cellspacing="0" cellpadding="3">';
		// head
		$html .= '<tr bgcolor="#ddeeff" height="25"><th scope="col">序号</th>';
		foreach ( $keyinfo as $k => $v ) {
			$t = empty ( $unit [$k] ) ? '' : "&nbsp（$unit[$k]）";
			$b=0;
			if($_GET['o']==$k) {
				$b=$_GET['b']; 
				$b++ && ($b-=2); 
			} 
			$v = GetLink("?o=$k&b=$b".$param,$v);
			$html .= "<th scope=\"col\">$v$t</th>";
		}
		$html .= '<th colspan="3" scope="col">操作</th></tr>';
		// body
		foreach ( $value as $k => $v ) {
			$html .= '<tr align="center">';
			$html .= '<td>' . ($k + 1) . '</td>';
			foreach ( $keyinfo as $ki => $vi ) {
				$html .= "<td>$v[$ki]</td>";
			}
			$id = array_shift ( $v );
			$html .= '<td><strong>' . GetLink ( "$op?act=v&id=$id", '查看' ) . '</strong></td>';
			$html .= '<td><strong>' . GetLink ( "$op?act=e&id=$id", '修改' ) . '</strong></td>';
			$html .= '<td><strong>' . GetLink ( "$op?act=d&id=$id", '删除' ) . '</strong></td>';
			$html .= '</tr>';
		}
		$html .= '</table>';
		// other
		$html .= '<div id="create">' . GetLink ( "$op?act=c", '添加' ) . '</div>';
		echo $html;
	}
}

if (! function_exists ( 'GetInput' )) {
	function GetInput($name, $value = '', $size = 12) {
		return "<input name=\"$name\" type=\"text\" value=\"$value\" size=\"$size\" />";
	}
}

if (! function_exists ( 'GetFile' )) {
	function GetFile($name, $value = '' ,$accept='') {
		return "<input name=\"$name\" type=\"file\" value=\"$value\" $accept />";
	}
}

if (! function_exists ( 'GetImage' )) {
	function GetImage($src,$width=300,$height=240) {
		return "<img src=\"$src\" width=\"$width\" height=\"$height\" />";
	}
}

if (! function_exists ( 'GetTextArea' )) {
	function GetTextArea($name, $value = '', $r = 5, $c = 30) {
		return "<textarea name=\"$name\" cols=\"$c\" rows=\"$r\">$value</textarea>";
	}
}

if (! function_exists ( 'GetTimeInput' )) {
	function GetTimeInput($name, $value = '') {
		return "<input name=\"$name\" type=\"text\" value=\"$value\" size=\"10\" />";
	}
}

if (! function_exists ( 'GetSubmit' )) {
	function GetSubmit() {
		return "<input type=\"submit\" name=\"submit\" value=\"提交\" />";
	}
}

if (! function_exists ( 'GetReset' )) {
	function GetReset() {
		return "<input type=\"reset\" name=\"Submit2\" value=\"重置\" />";
	}
}
if (! function_exists ( 'GetForm' )) {
	function GetForm($innerHTML, $action = '', $name = 'form1', $others = 'enctype="multipart/form-data"') {
		return "<form id=\"form1\" name=\"$name\" method=\"post\" $action=\"\" $others >$innerHTML</form>";
	}
}

if (! function_exists ( 'GetHidden' )) {
	function GetHidden($a) {
		$htm = '';
		foreach ( $a as $k => $v ) {
			$htm .= "<input name=\"$k\" type=\"hidden\" value=\"$v\" />";
		}
		return $htm;
	}
}

if (! function_exists ( 'GetDiv' )) {
	function GetDiv($id, $value) {
		return "<div id=\"$id\">$value</div>";
	}
}

if (! function_exists ( 'GetLink' )) {
	function GetLink($href, $value) {
		return "<a href=\"$href\">$value</a>";
	}
}

if (! function_exists ( 'MyEcho' )) {
	function MyEcho($mixd) {
		echo '<br/><pre>';
		var_export ( $mixd );
		echo '</pre><br/>';
	}
}

if (! function_exists ( 'GetHead' )) {
	function GetHead($str=''){
		empty($str) && $str='信息管理';
		$html='<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>'.$str.'</title></head><body>';
		return $html;
	}
}

if (! function_exists ( 'GetFoot' )) {
	function GetFoot(){
		$html='<body></body></html>';
		return $html;
	}
}

/**
 * trans array to html for input view
 * @param string $name;
 * @param array $arr as array( [@]name => value );
 * @param int $size
 */
function GetSelect($name,$arr,$size=0){
	$ext=$size==0?'"':'[]" size="'.$size.'" multiple="multiple"';
	$h="<select name=\"$name$ext>";
	if (empty($arr)){
		$h.='<option value="NULL">无数据</option>';
	}else {
		foreach ($arr as $k => $v) {
			(substr($k,0,1)=='@') && $k=substr($k,1).'" selected="selected'; 
			$h.="<option value=\"$k\">$v</option>";
		}
	}
	$h.='</select>';
	return $h;
}
